home *** CD-ROM | disk | FTP | other *** search
/ PC Open 101 / PC Open 101 CD 1.bin / CD1 / INTERNET / EMAIL / pop file / setup.exe / Proxy / NNTP.pm next >
Encoding:
Perl POD Document  |  2004-08-06  |  16.1 KB  |  502 lines

  1. # POPFILE LOADABLE MODULE
  2. package Proxy::NNTP;
  3.  
  4. use Proxy::Proxy;
  5. @ISA = ("Proxy::Proxy");
  6.  
  7. # ---------------------------------------------------------------------------------------------
  8. #
  9. # This module handles proxying the NNTP protocol for POPFile.
  10. #
  11. # Copyright (c) 2001-2004 John Graham-Cumming
  12. #
  13. #   This file is part of POPFile
  14. #
  15. #   POPFile is free software; you can redistribute it and/or modify
  16. #   it under the terms of the GNU General Public License as published by
  17. #   the Free Software Foundation; either version 2 of the License, or
  18. #   (at your option) any later version.
  19. #
  20. #   POPFile is distributed in the hope that it will be useful,
  21. #   but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23. #   GNU General Public License for more details.
  24. #
  25. #   You should have received a copy of the GNU General Public License
  26. #   along with POPFile; if not, write to the Free Software
  27. #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. #
  29. #   Modified by     Sam Schinke (sschinke@users.sourceforge.net)
  30. #
  31. # ---------------------------------------------------------------------------------------------
  32.  
  33. use strict;
  34. use warnings;
  35. use locale;
  36.  
  37. # A handy variable containing the value of an EOL for networks
  38.  
  39. my $eol = "\015\012";
  40.  
  41. #----------------------------------------------------------------------------
  42. # new
  43. #
  44. #   Class new() function
  45. #----------------------------------------------------------------------------
  46. sub new
  47. {
  48.     my $type = shift;
  49.     my $self = Proxy::Proxy->new();
  50.  
  51.     # Must call bless before attempting to call any methods
  52.  
  53.     bless $self, $type;
  54.  
  55.     $self->name( 'nntp' );
  56.  
  57.     $self->{child_} = \&child__;
  58.     $self->{connection_timeout_error_} = '500 no response from mail server';
  59.     $self->{connection_failed_error_}  = '500 can\'t connect to';
  60.     $self->{good_response_}            = '^(1|2|3)\d\d';
  61.  
  62.     return $self;
  63. }
  64.  
  65. # ---------------------------------------------------------------------------------------------
  66. #
  67. # initialize
  68. #
  69. # Called to initialize the NNTP proxy module
  70. #
  71. # ---------------------------------------------------------------------------------------------
  72. sub initialize
  73. {
  74.     my ( $self ) = @_;
  75.  
  76.     # Disabled by default
  77.  
  78.     $self->config_( 'enabled', 0);
  79.  
  80.     # By default we don't fork on Windows
  81.  
  82.     $self->config_( 'force_fork', ($^O eq 'MSWin32')?0:1 );
  83.  
  84.     # Default ports for NNTP service and the user interface
  85.  
  86.     $self->config_( 'port', 119 );
  87.  
  88.     # Only accept connections from the local machine for NNTP
  89.  
  90.     $self->config_( 'local', 1 );
  91.  
  92.     # The separator within the NNTP user name is :
  93.  
  94.     $self->config_( 'separator', ':');
  95.  
  96.     # The welcome string from the proxy is configurable
  97.  
  98.     $self->config_( 'welcome_string',
  99.         "NNTP POPFile ($self->{version_}) server ready" );
  100.  
  101.     if ( !$self->SUPER::initialize() ) {
  102.         return 0;
  103.     }
  104.  
  105.     $self->config_( 'enabled', 0 );
  106.  
  107.     return 1;
  108. }
  109.  
  110. # ---------------------------------------------------------------------------------------------
  111. #
  112. # start
  113. #
  114. # Called to start the NNTP proxy module
  115. #
  116. # ---------------------------------------------------------------------------------------------
  117. sub start
  118. {
  119.     my ( $self ) = @_;
  120.  
  121.     # If we are not enabled then no further work happens in this module
  122.  
  123.     if ( $self->config_( 'enabled' ) == 0 ) {
  124.         return 2;
  125.     }
  126.  
  127.     # Tell the user interface module that we having a configuration
  128.     # item that needs a UI component
  129.  
  130.     $self->register_configuration_item_( 'configuration',
  131.                                          'nntp_port',
  132.                                          'nntp-port.thtml',
  133.                                          $self );
  134.  
  135.     $self->register_configuration_item_( 'configuration',
  136.                                          'nntp_force_fork',
  137.                                          'nntp-force-fork.thtml',
  138.                                          $self );
  139.  
  140.     $self->register_configuration_item_( 'configuration',
  141.                                          'nntp_separator',
  142.                                          'nntp-separator.thtml',
  143.                                          $self );
  144.  
  145.     $self->register_configuration_item_( 'security',
  146.                                          'nntp_local',
  147.                                          'nntp-security-local.thtml',
  148.                                          $self );
  149.  
  150.     return $self->SUPER::start();; }
  151.  
  152. # ---------------------------------------------------------------------------------------------
  153. #
  154. # child__
  155. #
  156. # The worker method that is called when we get a good connection from a client
  157. #
  158. # $client   - an open stream to a NNTP client
  159. # $session        - API session key
  160. #
  161. # ---------------------------------------------------------------------------------------------
  162. sub child__
  163. {
  164.     my ( $self, $client, $session ) = @_;
  165.  
  166.     # Number of messages downloaded in this session
  167.  
  168.     my $count = 0;
  169.  
  170.     # The handle to the real news server gets stored here
  171.  
  172.     my $news;
  173.  
  174.     # The state of the connection (username needed, password needed,
  175.     # authenticated/connected)
  176.  
  177.     my $connection_state = 'username needed';
  178.  
  179.     # Tell the client that we are ready for commands and identify our
  180.     # version number
  181.  
  182.     $self->tee_( $client, "201 " . $self->config_( 'welcome_string' ) .
  183.         "$eol" );
  184.  
  185.     # Retrieve commands from the client and process them until the
  186.     # client disconnects or we get a specific QUIT command
  187.  
  188.     while  ( <$client> ) {
  189.         my $command;
  190.  
  191.         $command = $_;
  192.  
  193.         # Clean up the command so that it has a nice clean $eol at the end
  194.  
  195.         $command =~ s/(\015|\012)//g;
  196.  
  197.         $self->log_( 2, "Command: --$command--" );
  198.  
  199.         # The news client wants to stop using the server, so send that
  200.         # message through to the real news server, echo the response
  201.         # back up to the client and exit the while.  We will close the
  202.         # connection immediately
  203.  
  204.         if ( $command =~ /^ *QUIT/i ) {
  205.             if ( $news )  {
  206.                 last if ( $self->echo_response_( $news, $client, $command ) ==
  207.                          2 );
  208.                 close $news;
  209.             } else {
  210.                 $self->tee_( $client, "205 goodbye$eol" );
  211.             }
  212.             last;
  213.         }
  214.  
  215.         if ($connection_state eq 'username needed') {
  216.  
  217.             # NOTE: This syntax is ambiguous if the NNTP username is a
  218.             # short (under 5 digit) string (eg, 32123).  If this is
  219.             # the case, run "perl popfile.pl -nntp_separator /" and
  220.             # change your kludged username appropriately (syntax would
  221.             # then be server[:port][/username])
  222.  
  223.             my $user_command = '^ *AUTHINFO USER ([^:]+)(:([\d]{1,5}))?(\\' .
  224.                 $self->config_( 'separator' ) . '(.+))?';
  225.  
  226.             if ( $command =~ /$user_command/i ) {
  227.                 my $server = $1;
  228.  
  229.                 # hey, the port has to be in range at least
  230.  
  231.                 my $port = $3 if ( defined($3) && ($3 > 0) && ($3 < 65536) );
  232.                 my $username = $5;
  233.  
  234.                 if ( $server ne '' )  {
  235.                     if ( $news = $self->verify_connected_( $news, $client,
  236.                         $server, $port || 119 ) )  {
  237.                         if (defined $username) {
  238.  
  239.                             # Pass through the AUTHINFO command with
  240.                             # the actual user name for this server, if
  241.                             # one is defined, and send the reply
  242.                             # straight to the client
  243.  
  244.                             $self->get_response_( $news, $client,
  245.                                 'AUTHINFO USER ' . $username );
  246.                             $connection_state = "password needed";
  247.                         } else {
  248.  
  249.                             # Signal to the client to send the password
  250.  
  251.                             $self->tee_($client, "381 password$eol");
  252.                             $connection_state = "ignore password";
  253.                         }
  254.                     } else {
  255.                         last;
  256.                     }
  257.                 } else {
  258.                     $self->tee_( $client,
  259.                         "482 Authentication rejected server name not specified in AUTHINFO USER command$eol" );
  260.                     last;
  261.                 }
  262.  
  263.                 $self->flush_extra_( $news, $client, 0 );
  264.                 next;
  265.             } else {
  266.  
  267.                 # Issue a 480 authentication required response
  268.  
  269.                 $self->tee_( $client, "480 Authorization required for this command$eol" );
  270.                 next;
  271.             }
  272.         } elsif ( $connection_state eq "password needed" ) {
  273.             if ($command =~ /^ *AUTHINFO PASS (.*)/i) {
  274.                 my ( $response, $ok ) = $self->get_response_( $news, $client,
  275.                                             $command);
  276.  
  277.                 if ($response =~ /^281 .*/) {
  278.                     $connection_state = "connected";
  279.                 }
  280.                 next;
  281.             } else {
  282.  
  283.                 # Issue a 381 more authentication required response
  284.  
  285.                 $self->tee_( $client, "381 more authentication required for this command$eol" );
  286.                 next;
  287.             }
  288.         } elsif ($connection_state eq "ignore password") {
  289.             if ($command =~ /^ *AUTHINFO PASS (.*)/i) {
  290.                 $self->tee_($client, "281 authentication accepted$eol");
  291.                 $connection_state = "connected";
  292.                 next;
  293.             } else {
  294.  
  295.                 # Issue a 480 authentication required response
  296.  
  297.                 $self->tee_( $client, "381 more authentication required for this command$eol" );
  298.                 next;
  299.             }
  300.         } elsif ( $connection_state eq "connected" ) {
  301.  
  302.             # COMMANDS USED DIRECTLY WITH THE REMOTE NNTP SERVER GO HERE
  303.  
  304.             # The client wants to retrieve an article. We oblige, and
  305.             # insert classification headers.
  306.  
  307.             if ( $command =~ /^ *ARTICLE (.*)/i ) {
  308.                 my ( $response, $ok ) = $self->get_response_( $news, $client,
  309.                                             $command);
  310.                 if ( $response =~ /^220 (.*) (.*)$/i) {
  311.                     $count += 1;
  312.  
  313.                     my ( $class, $history_file ) =
  314.                         $self->{classifier__}->classify_and_modify( $session,
  315.                             $news, $client, 0, '', 0 );
  316.                 }
  317.  
  318.                 next;
  319.             }
  320.  
  321.             # Commands expecting a code + text response
  322.  
  323.             if ( $command =~ 
  324.                 /^ *(LIST|HEAD|BODY|NEWGROUPS|NEWNEWS|LISTGROUP|XGTITLE|XINDEX|XHDR|XOVER|XPAT|XROVER|XTHREAD)/i ) {
  325.                 my ( $response, $ok ) = $self->get_response_( $news,
  326.                                             $client, $command);
  327.  
  328.                 # 2xx (200) series response indicates multi-line text
  329.                 # follows to .crlf
  330.  
  331.                 if ( $response =~ /^2\d\d/ ) {
  332.                     $self->echo_to_dot_( $news, $client, 0 );
  333.                 }
  334.                 next;
  335.             }
  336.  
  337.             # Exceptions to 200 code above
  338.  
  339.             if ( $ command =~ /^ *(HELP)/i ) {
  340.                 my ( $response, $ok ) = $self->get_response_( $news, $client,
  341.                                             $command);
  342.                 if ( $response =~ /^1\d\d/ ) {
  343.                     $self->echo_to_dot_( $news, $client, 0 );
  344.                 }
  345.                 next;
  346.             }
  347.  
  348.             # Commands expecting a single-line response
  349.  
  350.             if ( $command =~ 
  351.                 /^ *(GROUP|STAT|IHAVE|LAST|NEXT|SLAVE|MODE|XPATH)/i ) {
  352.                 $self->get_response_( $news, $client, $command );
  353.                 next;
  354.             }
  355.  
  356.             # Commands followed by multi-line client response
  357.  
  358.             if ( $command =~ /^ *(IHAVE|POST|XRELPIC)/i ) {
  359.                 my ( $response, $ok ) = $self->get_response_( $news, $client,
  360.                                             $command);
  361.  
  362.                 # 3xx (300) series response indicates multi-line text
  363.                 # should be sent, up to .crlf
  364.  
  365.                 if ($response =~ /^3\d\d/ ) {
  366.  
  367.                     # Echo from the client to the server
  368.  
  369.                     $self->echo_to_dot_( $client, $news, 0 );
  370.  
  371.                     # Echo to dot doesn't provoke a server response
  372.                     # somehow, we add another CRLF
  373.  
  374.                     $self->get_response_( $news, $client, "$eol" );
  375.                 }
  376.                 next;
  377.             }
  378.         }
  379.  
  380.         # Commands we expect no response to, such as the null command
  381.  
  382.         if ( $ command =~ /^ *$/ ) {
  383.             if ( $news && $news->connected ) {
  384.                 $self->get_response_( $news, $client, $command, 1 );
  385.                 next;
  386.             }
  387.         }
  388.  
  389.         # Don't know what this is so let's just pass it through and
  390.         # hope for the best
  391.  
  392.         if ( $news && $news->connected)  {
  393.             $self->echo_response_($news, $client, $command );
  394.             next;
  395.         } else {
  396.             $self->tee_(  $client, "500 unknown command or bad syntax$eol" );
  397.             last;
  398.         }
  399.     }
  400.  
  401.     if ( defined( $news ) ) {
  402.         $self->done_slurp_( $news );
  403.         close $news;
  404.     }
  405.     close $client;
  406.     $self->mq_post_( 'CMPLT', $$ );
  407.     $self->log_( 0, "NNTP proxy done" );
  408. }
  409.  
  410. # ---------------------------------------------------------------------------------------------
  411. #
  412. # configure_item
  413. #
  414. #    $name            Name of this item
  415. #    $templ           The loaded template that was passed as a parameter
  416. #                     when registering
  417. #    $language        Current language
  418. #
  419. # ---------------------------------------------------------------------------------------------
  420.  
  421. sub configure_item
  422. {
  423.     my ( $self, $name, $templ, $language ) = @_;
  424.  
  425.     if ( $name eq 'nntp_port' ) {
  426.         $templ->param( 'nntp_port' => $self->config_( 'port' ) );
  427.     }
  428.  
  429.     # Separator Character widget
  430.     if ( $name eq 'nntp_separator' ) {
  431.         $templ->param( 'nntp_separator' => $self->config_( 'separator' ) );
  432.     }
  433.  
  434.     if ( $name eq 'nntp_local' ) {
  435.         $templ->param( 'nntp_if_local' => $self->config_( 'local' ) );
  436.      }
  437.  
  438.     if ( $name eq 'nntp_force_fork' ) {
  439.         $templ->param( 'nntp_force_fork_on' => $self->config_( 'force_fork' ) );
  440.     }
  441.  
  442.     #$self->SUPER::configure_item( $name, $language, $session_key );
  443. }
  444.  
  445. # ---------------------------------------------------------------------------------------------
  446. #
  447. # validate_item
  448. #
  449. #    $name            The name of the item being configured, was passed in by the call
  450. #                     to register_configuration_item
  451. #    $templ           The loaded template
  452. #    $language        The language currently in use
  453. #    $form            Hash containing all form items
  454. #
  455. # ---------------------------------------------------------------------------------------------
  456.  
  457. sub validate_item
  458. {
  459.     my ( $self, $name, $templ, $language, $form ) = @_;
  460.  
  461.     if ( $name eq 'nntp_port' ) {
  462.         if ( defined $$form{nntp_port} ) {
  463.             if ( ( $$form{nntp_port} >= 1 ) && ( $$form{nntp_port} < 65536 ) ) {
  464.                 $self->config_( 'port', $$form{nntp_port} );
  465.                 $templ->param( 'nntp_port_feedback' => sprintf $$language{Configuration_NNTPUpdate}, $self->config_( 'port' ) );
  466.              } 
  467.              else {
  468.                  $templ->param( 'nntp_port_feedback' => "<div class=\"error01\">$$language{Configuration_Error3}</div>" );
  469.              }
  470.         }
  471.     }
  472.  
  473.     if ( $name eq 'nntp_separator' ) {
  474.         if ( defined $$form{nntp_separator} ) {
  475.             if ( length($$form{nntp_separator}) == 1 ) {
  476.                 $self->config_( 'separator', $$form{nntp_separator} );
  477.                 $templ->param( 'nntp_separator_feedback' => sprintf $$language{Configuration_NNTPSepUpdate}, $self->config_( 'separator' ) );
  478.             } 
  479.             else {
  480.                 $templ->param( 'nntp_separator_feedback' => "<div class=\"error01\">\n$$language{Configuration_Error1}</div>\n" );
  481.             }
  482.         }
  483.     }
  484.  
  485.     if ( $name eq 'nntp_local' ) {
  486.         if ( defined $$form{nntp_local} ) {
  487.             $self->config_( 'local', $$form{nntp_local} );
  488.         }
  489.     }
  490.  
  491.  
  492.     if ( $name eq 'nntp_force_fork' ) {
  493.         if ( defined $$form{nntp_force_fork} ) {
  494.             $self->config_( 'force_fork', $$form{nntp_force_fork} );
  495.         }
  496.     }
  497.  
  498.     # $self->SUPER::validate_item( $name, $language, $form );
  499. }
  500.  
  501. 1;
  502.